<?php
/**
* WDC Like Dislike
* @author WDC <contact@web-designs-company.com>
* @link http://www.web-designs-company.com/
* @copyright (C) 2011 - 2012 JExtension
* @license GNU/GPL, see LICENSE.php for full license.
**/
 

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view' );
jimport('joomla.html.pane');

class  wdclikedislikeViewwdclikedislike  extends JView
{
	function display($tpl = null)
	{
		global $mainframe;

		$db =& JFactory::getDBO();

		$model = & $this->getModel();

		if($this->_layout == 'details')
		{
			$studentsRecord =& $this->get('Data');

			$this->assignRef('row',	$studentsRecord);
		}
		else
		{
			$context			= 'com_wdclikedislike.s.id.list.';

			$filter_order		= $mainframe->getUserStateFromRequest( $context.'filter_order',		'filter_order',		's.id',	'cmd' );
			$filter_order_Dir	= $mainframe->getUserStateFromRequest( $context.'filter_order_Dir',	'filter_order_Dir',	'',			'word' );
			$filter_state		= $mainframe->getUserStateFromRequest( $context.'filter_state',		'filter_state',		'',			'word' );
			$search				= $mainframe->getUserStateFromRequest( $context.'search',			'search',			'',			'string' );

			$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
			$limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' );

			$where = array();

			if ( $filter_state )
			{
				if ( $filter_state == 'P' ) {
					$where[] = 's.state = 1';
				}
				else if ($filter_state == 'U' ) {
					$where[] = 's.state = 0';
				}
			}

			if ($search)
			{
				$where[] = 'LOWER(s.title) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );

			}

			$where		= count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '';
			$orderby	= ' ORDER BY '. $filter_order .' '. $filter_order_Dir .', s.id';

			// get the total number of records
			$query = 'SELECT count(*) FROM #__content AS s'
				. $where
				;
			$db->setQuery( $query );
			if (!$db->query())
			{
				echo $db->getErrorMsg();
			}

			$total = $db->loadResult();

			jimport('joomla.html.pagination');
			$pageNav = new JPagination( $total, $limitstart, $limit );

						if($search==""){
			$where='WHERE (#__cvote.article_id=#__content.id)';
		   	$query = 'SELECT #__cvote.article_id FROM #__cvote,#__content ' .$where	 ;
			$db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
			$row = $db->loadObjectList();
			foreach($row as $row):
				 $query="SELECT * FROM #__content WHERE id='$row->article_id'";
				 $db->setQuery( $query);$rows = $db->loadObjectList();

			 	 $query1="SELECT COUNT(*) FROM #__cvote WHERE article_id='$row->article_id' AND dlike='1'";
				 $db->setQuery( $query1);$rows1 = $db->loadResult();

		    	 $query2="SELECT COUNT( * ) FROM #__cvote WHERE `like` = '1' AND `article_id` = '$row->article_id'";
				 $db->setQuery( $query2);  $rows2 = $db->loadResult();


			endforeach;
			}else{
			$query = 'SELECT s.* FROM #__content AS s '
			. $where
			. $orderby
			;
			$db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
			$rows = $db->loadObjectList();
			}

			// state filter
			$lists['state']	= JHTML::_('grid.state',  $filter_state );

			// table ordering
			$lists['order_Dir']	= $filter_order_Dir;
			$lists['order']		= $filter_order;

			// search filter
			$lists['search']= $search;

			$this->assignRef('items2',		$rows2);
			$this->assignRef('items1',		$rows1);
			$this->assignRef('items',		$rows);
			$this->assignRef('pageNav',		$pageNav);
			$this->assignRef('lists',		$lists);
		}

		parent::display($tpl);
	}
}